package com.yaorange.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.yaorange.dao.CustomerMapper;
import com.yaorange.entity.Customer;
import com.yaorange.service.CustomerService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import tk.mybatis.mapper.entity.Example;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Set;

@Service
@Transactional
public class CustomerServiceImpl implements CustomerService {


    @Resource
    private CustomerMapper customerMapper;

    @Override
    public List<String> getRepeatPhones(Set<String> importCustomerPhones) {
        //自定义语句返回单列数据集合
        return customerMapper.selectPhonesIn(importCustomerPhones);
    }

    @Override
    public int importList(Map<String, Customer> importCustomerMap) {
        //转换Map->List
        ArrayList<Customer> customers = new ArrayList<>(importCustomerMap.values());
        //使用tk-mapper的批量新增方法
        return customerMapper.insertList(customers);
    }

    @Override
    public int add(Customer customer) {
        return customerMapper.insert(customer);
    }

    @Override
    public PageInfo<Customer> customerList(Integer pageNum, Integer pageSize, Integer empId) {
        //设置分页条件
        PageHelper.startPage(pageNum,pageSize);
        //调用查询
        Example example = new Example(Customer.class);
        Example.Criteria criteria = example.createCriteria();

        criteria.andEqualTo("addPersonId",empId);//自己添加
        criteria.andEqualTo("addType",0);//市场录入
        criteria.andEqualTo("visibility",0);//自留客户

        List<Customer> customerList = customerMapper.selectByExample(example);
        return new PageInfo<>(customerList);
    }

    @Override
    public int update(Customer customer) {
        return customerMapper.updateByPrimaryKey(customer);
    }

    @Override
    public List<Map<String, Object>> getRosterList(Integer empId) {
        return customerMapper.selectRosterList(empId);
    }

    @Override
    public PageInfo<Customer> getRosterPage(Integer pageNum, Integer pageSize, String rosterName) {
        //设置分页条件
        PageHelper.startPage(pageNum,pageSize);
        //调用查询语句：只能获取状态为0或1的部门
        List<Customer> customerList = customerMapper.selectRosterCustomerByXml(rosterName);
        return new PageInfo<>(customerList);
    }

    @Override
    public int updateCustomerByIds(List<Integer> customerIds, Integer deptId) {
        return customerMapper.updateCustomerByIds(customerIds,deptId);
    }

    @Override
    public int updateCustomerByRosterName(String rosterName, Integer deptId) {
        return customerMapper.updateCustomerByRosterName(rosterName,deptId);
    }

    @Override
    public List<Map<String, Object>> getWeekData() {
        return customerMapper.selectWeeklyData(1);
    }

    @Override
    public List<Map<String, Object>> getYearAndMonthData(int addType, String monthValue) {
        return customerMapper.selectYearAndMonthData(addType,monthValue);
    }

    @Override
    public PageInfo<Customer> getNewOrderList(Integer pageNum, Integer pageSize, Integer empId) {
        PageHelper.startPage(pageNum,pageSize);
        List<Customer> customerList = customerMapper.selectNewOrderByXml(empId);
        return customerList.size() == 0 ? null : new PageInfo<>(customerList);
    }

    @Override
    public Customer getCustomerById(Integer customerId) {
        return customerMapper.selectByPrimaryKey(customerId);
    }

    @Override
    public int updateCustomer(Customer customer) {
        return customerMapper.updateByPrimaryKeySelective(customer);
    }

    @Override
    public PageInfo<Customer> getFollowPage(Integer pageNum, Integer pageSize, Integer empId) {
        PageHelper.startPage(pageNum,pageSize);
        List<Customer> customerList = customerMapper.selectFollowByXml(empId);
        if(customerList.size() == 0){
            return null;
        }
        return new PageInfo<>(customerList);
    }

    @Override
    public int updateById(Integer customerId) {
        return customerMapper.updateVisibilityByXml(customerId);
    }

    @Override
    public PageInfo<Customer> getValidPage(Integer pageNum, Integer pageSize, Integer empId) {
        PageHelper.startPage(pageNum,pageSize);
        List<Customer> customerList = customerMapper.selectValidByXml(empId);
        if(customerList.size() == 0){
            return null;
        }
        return new PageInfo<>(customerList);
    }

    @Override
    public Customer getCustomerAndPropertyById(Integer id) {
        return customerMapper.selectCustomerByXml(id);
    }

    @Override
    public int updateBySelective(Customer customer) {
        return customerMapper.updateByPrimaryKeySelective(customer);
    }

    @Override
    public int updateCustomerWithOrder(Customer customer) {
        return customerMapper.updateByPrimaryKeySelective(customer);
    }

    @Override
    public PageInfo<Customer> getPageByDeptId(Integer pageNum, Integer pageSize, Integer deptId) {
        PageHelper.startPage(pageNum,pageSize);
        List<Customer>  customerList=customerMapper.selectByDeptId(deptId);
        return new PageInfo<>(customerList);
    }

    @Override
    public PageInfo<Customer> getInvalidPageByDeptId(Integer pageNum, Integer pageSize, Integer deptId) {
        PageHelper.startPage(pageNum,pageSize);
        List<Customer>  customerList=customerMapper.selectInvalidByDeptId(deptId);
        return new PageInfo<>(customerList);
    }

    //已签单客户分页数据
    @Override
    public PageInfo<Customer> getSigendPage(Integer pageNum, Integer pageSize, Integer empId) {
        PageHelper.startPage(pageNum,pageSize);
        List<Customer>  customerList=customerMapper.selectSignedCustomer();
        return new PageInfo<>(customerList);
    }

    @Override
    public Customer getSignedCustomerById(Integer id) {
        return customerMapper.selectSignedCustomerByXml(id);
    }


}
